﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;
using WMSProject.XiangHe.Procurements;

namespace WMSProject.SuJiaHao
{
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class LossReportingService : ApplicationService, ILossReportingService
    {
        /// <summary>
        /// 报损管理仓储
        /// </summary>
        private readonly IRepository<LossReportingManagement, int> _lossRepositoryRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="lossRepositoryRepository">报损管理仓储</param>
        public LossReportingService(IRepository<LossReportingManagement, int> lossRepositoryRepository)
        {
            _lossRepositoryRepository = lossRepositoryRepository;
        }

        /// <summary>
        /// 获取销售退货列表
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns>返回销售退货的对象把</returns>
        public async Task<ReturnSelect<object>> GetLossReportingList(string reportNumber, int pageSize, int pageIndex)
        {
            // 获取生产领料列表
            var lossReportingList = await _lossRepositoryRepository.GetListAsync();

            // 使用 WhereIf 进行条件筛选
            var lossReportingLists = lossReportingList
                .WhereIf(!string.IsNullOrEmpty(reportNumber), x => x.ReportNumber==reportNumber)
                .ToList();

            // 计算总记录数和当前页记录
            var totalCount = lossReportingLists.Count;
            var pageList = lossReportingLists.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<LossReportingManagement>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 报损管理添加
        /// </summary>
        /// <param name="input">报损管理实体</param>
        /// <returns></returns>
        public async Task<int> GetCreateLossReporting(CreateLossReportingDTO input)
        {
            var lossReportingList = ObjectMapper.Map<CreateLossReportingDTO, LossReportingManagement>(input);
            lossReportingList.Creator = "崔二强";
            lossReportingList.ModifiedBy = "崔大强";
            lossReportingList.Remark = "111";
            //sasles.CreateBy = "崔二强";
            //sasles.CreateTime = DateTime.Now;
            await _lossRepositoryRepository.InsertAsync(lossReportingList);
            return lossReportingList != null ? 1 : 0;
        }

        /// <summary>
        /// 报损管理信息删除
        /// </summary>
        /// <param name = "id" > 条件编号 </ param >
        /// < returns > 返回受影响行数 </ returns >
        public async Task<int> DeleteLossReporting(int id)
        {
            var lossReportingDel = await _lossRepositoryRepository.GetAsync(id);
            await _lossRepositoryRepository.DeleteAsync(lossReportingDel);
            return lossReportingDel != null ? 1 : 0;
        }

        /// <summary>
        /// 报损管理反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回报损管理实体</returns>
        public async Task<LossReportingManagement> GetLossReportingManagement(int id)
        {
            var lossReportings = await _lossRepositoryRepository.FirstOrDefaultAsync(x => x.Id == id);
            return lossReportings;
        }

        /// <summary>
        /// 销售退货信息修改
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateLossReporting(CreateLossReportingDTO input)
        {
            var lossReporting = ObjectMapper.Map<CreateLossReportingDTO, LossReportingManagement>(input);
            lossReporting.Creator = "崔二强";
            lossReporting.ModifiedBy = "崔大强";
            lossReporting.Remark = "111";
            await _lossRepositoryRepository.UpdateAsync(lossReporting);
            return lossReporting != null ? 1 : 0;
        }
    }
}
